package com.aabte.testng.paramter;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

/**
 * 参数化测试：代码内定义数据源
 */
public class DataProviderTest {

    /**
     * 指定参数源
     */
    @Test(dataProvider = "data")
    public void testDataProvider(String name, int age){
        System.out.println("name = " + name + ", age = " + age);
    }

    /**
     * 定义参数源
     */
    @DataProvider(name = "data")
    public Object[][] providerData(){
        Object[][] objects = new Object[][] {
                {"zhangsan", 18},
                {"lisi", 20},
                {"wangwu", 30}
        };
        return objects;
    }

    /**
     * 根据测试方法的不同，数据源返回不同的测试数据
     */
    @Test(dataProvider = "methodData")
    public void test1(String name, int age){
        System.out.println("run test1  name = " + name + ", age = " + age);
    }

    @Test(dataProvider = "methodData")
    public void test2(String name, int age){
        System.out.println("run test2  name = " + name + ", age = " + age);
    }

    @DataProvider(name = "methodData")
    public Object[][] methodData(Method method){

        Object[][] result = null;
        if ("test1".equals(method.getName())) {
            result = new Object[][] {
                    {"zhangsan", 18},
                    {"lisi", 20},
                    {"wangwu", 30}
            };
        } else if ("test2".equals(method.getName())) {
            result = new Object[][] {
                    {"zhangsan", 20},
                    {"lisi", 22},
                    {"wangwu", 32}
            };
        }
        return result;
    }

}
